#include "Tiles.h"


Tiles::Tiles(void)
{
	Rows = 0;
	Cols = 0;
}

Tiles::Tiles(int rows, int columns)
{
	Rows = rows;
	Cols = columns;

	RenderHelper::worldsEndX = (rows * 32) - 800;
	RenderHelper::worldsEndY = (columns * 32) - 600;
}

Tiles::Tiles(int rows, int columns, int**& map)
{
	Rows = rows;
	Cols = columns;
	tiles = map;

	RenderHelper::worldsEndX = (rows * 32) - 800;
	RenderHelper::worldsEndY = (columns * 32) - 600;
}

void Tiles::Draw(HDC bufDC)
{
	if (tiles != 0) {
		for (int i = 0; i < Rows; i++) {
			for (int j = 0; j < Cols; j++) {
				if(!(tiles[i][j] < 0)){
					renderer.DrawTile(bufDC,tiles,  i,j);
				}
			}
		}
	}
}


bool Tiles::canGoThere(int x, int y, int width, int height){
	//int tileXLeft = (((x * 10) / Constants.TILEWIDTH) - (((x * 10) / Constants.TILEWIDTH) % 10)) / 10;
	//int tileYTop =   (((y * 10) / Constants.TILEWIDTH) - (((y * 10) / Constants.TILEWIDTH) % 10)) / 10;
	//int tileXRight =   ((((x + width) * 10) / Constants.TILEWIDTH) + 10 - ((((x + width) * 10) / Constants.TILEWIDTH) % 10)) / 10;
	//int tileYBottom = ((((y + height) * 10) / Constants.TILEWIDTH) + 10 - ((((y + height) * 10) / Constants.TILEWIDTH) % 10)) / 10;

	float xFloat = (float)x;
	float tileWidthFloat = (float)Constants.TILEWIDTH;
	

	int tileXLeft = ((x / Constants.TILEWIDTH));
	int tileYTop =   y  / Constants.TILEWIDTH;
	int tileXRight =   ( ((double)x+0.99) + width) / Constants.TILEWIDTH;
	int tileYBottom = (  ((double)y+0.99) + height)/ Constants.TILEWIDTH;

	//if the tile is out of bounds
	if(x < 0 || y < 0  || tileXRight >= Rows || tileXLeft >= Rows || tileYTop >= Cols || tileYBottom >= Cols ){ 
		return false;
	}
	//if the column of the left edge is the same as the column of the right 
	//edge, then theres no need to check both columns.
	if (tileXLeft != tileXRight)
    {
		//if the row of the bottom edge is the same as the row of the top 
		//edge, then theres no need to check both rows.
		if (tileYBottom != tileYTop)
        {
			int tile1 = tiles[tileXLeft][tileYTop];
			int tile2 = tiles[tileXRight][tileYTop];
			int tile3 = tiles[tileXRight][tileYBottom];
			int tile4 = tiles[tileXLeft][tileYBottom];


			if(tile1 != -1){
				if(renderer.myTiles[tile1].walkable == true){
					return false;
				}	
			}
			if(tile2 != -1){
				if(renderer.myTiles[tile2].walkable == true){
					return false;
				}	
			}
			if(tile3 != -1){
				if(renderer.myTiles[tile3].walkable == true){
					return false;
				}	
			}
			if(tile4 != -1){
				if(renderer.myTiles[tile4].walkable == true){
					return false;
				}	
			}
			return true;
        }
        else
        {
			int tile1 = tiles[tileXLeft][tileYTop];
			int tile2 = tiles[tileXRight][tileYTop];			
			if(tile1 != -1){
				if(renderer.myTiles[tile1].walkable == true){
					return false;
				}	
			}
			if(tile2 != -1){
				if(renderer.myTiles[tile2].walkable == true){
					return false;
				}	
			}
			return true;
        }

    }
    else
    {
		//if the row of the bottom edge is the same as the row of the top 
		//edge, then theres no need to check both rows.
        if (tileYBottom != tileYTop)
        {
			int tile1 = tiles[tileXRight][tileYTop];
			int tile2 = tiles[tileXRight][tileYBottom];
			if(tile1 != -1){
				if(renderer.myTiles[tile1].walkable == true){
					return false;
				}	
			}
			if(tile2 != -1){
				if(renderer.myTiles[tile2].walkable == true){
					return false;
				}	
			}
			return true;
        }
        else
        {
			int tile1 = tiles[tileXRight][tileYBottom];
			if(tile1 != -1){
				if(renderer.myTiles[tile1].walkable == true){
					return false;
				}	
			}
			return true;
        }
    }
}


Tiles::~Tiles(void)
{

	for (int i = 0; i < Rows; i++) {
	delete [] tiles[i];
		  tiles[i] = 0;
	}
	delete [] tiles;
	tiles = 0;

}

void Tiles::initArray(){
	tiles = new int*[Rows];
	if (tiles != 0) {
		for (int i = 0; i < Rows; i++) {
			 tiles[i] = new int[Cols];
		}
	}
}